package com.hs.android.fortress.intro;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.util.EntityUtils;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.hs.android.fortress.model.ExplosionInformation;
import com.hs.android.fortress.model.Room;
import com.hs.android.fortress.net.GetMethodCommand;
import com.hs.android.fortress.net.NetworkCommand;

import android.os.Handler;
import android.os.HandlerThread;
import android.os.Message;
import android.util.Log;

public class RoomGetter extends HandlerThread {

	private static final String TAG = "RoomGetter";
	private static final int ROOM_GET = 0;

	private static final String GET_POSITION_URL = "/room?number=";

	Handler mHandler;
	Handler mResponseHandler;
	Listener mListener;
	NetworkCommand mCommand;

	public interface Listener {
		void onRoomInformationGotten(Room room);
	}

	public void setListener(Listener listener) {
		mListener = listener;
	}

	public RoomGetter(Handler responseHandler) {
		super(TAG);
		mResponseHandler = responseHandler;
	}

	@Override
	protected void onLooperPrepared() {
		mHandler = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				if (msg.what == ROOM_GET) {
					int roomNumber = (Integer) msg.obj;
					handleRequest(roomNumber);
				}
			}
		};
	}

	public void queueRoomNumber(int roomNumber) {
		mHandler.obtainMessage(ROOM_GET, roomNumber).sendToTarget();
	}

	private void handleRequest(int roomNumber) {

		String result = null;

		mCommand = new GetMethodCommand(GET_POSITION_URL + roomNumber);

		try {
			HttpResponse response = mCommand.callMethod();

			// status 200 means "OK" - the request has succeeded
			if (response.getStatusLine().getStatusCode() != 200) {
				Log.d(TAG, "Status: "
						+ response.getStatusLine().getStatusCode());
				return;
			}

			HttpEntity entity = response.getEntity();
			result = EntityUtils.toString(entity);
			
			if (result != null) {
				Gson gson = new Gson();
				
				Room room = gson.fromJson(result, Room.class);
				
				
				mResponseHandler.post(new handlerPost(room));
			}

		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public void clearQueue() {
		mHandler.removeMessages(ROOM_GET);
	}
	
	private class handlerPost implements Runnable{

		private Room room;
		
		handlerPost(Room room) {
			this.room = room;
		}
		
		@Override
		public void run() {
			if(room != null){
				mListener.onRoomInformationGotten(room);
			}
		}
	}
}
